version 8
set matsize 800
set more off

use allwt_CART_full_d99, clear


sort _impute_

** Note: dfdx is the marginal effect 

su _impute_ fdeath dshk99 year prod ppn




xi: statsby dfdx_ltfp=(el(e(dfdx),1,1)) se_ltfp=(el(e(se_dfdx),1,1)), by(_impute_) saving(dprobit_ltfp, replace): dprobit fdeath ltfp i.year*i.prod , cluster(ppn)

xi: statsby dfdx_ltfp2=(el(e(dfdx),1,1)) se_ltfp2=(el(e(se_dfdx),1,1)), by(_impute_) saving(dprobit_ltfp2, replace): dprobit fdeath ltfp2 i.year*i.prod , cluster(ppn)

xi: statsby dfdx_ltfpphy=(el(e(dfdx),1,1)) se_ltfpphy=(el(e(se_dfdx),1,1)), by(_impute_) saving(dprobit_ltfpq, replace): dprobit fdeath ltfpphy i.year*i.prod , cluster(ppn)

xi: statsby dfdx_lprice2=(el(e(dfdx),1,1)) se_lprice2=(el(e(se_dfdx),1,1)), by(_impute_) saving(dprobit_lprice2, replace): dprobit fdeath lprice2 i.year*i.prod , cluster(ppn)

xi: statsby dfdx_dshk99=(el(e(dfdx),1,1)) se_dshk99=(el(e(se_dfdx),1,1)), by(_impute_) saving(dprobit_dshk99, replace): dprobit fdeath dshk99 i.year*i.prod , cluster(ppn)

xi: statsby dfdx_ltfpphy=(el(e(dfdx),1,1)) dfdx_lprice2=(el(e(dfdx),1,2)) se_ltfpphy=(el(e(se_dfdx),1,1)) se_lprice2=(el(e(se_dfdx),1,2)), by(_impute_) saving(dprobit_tfpq_lprice2, replace): dprobit fdeath ltfpphy lprice2 i.year*i.prod , cluster(ppn)

xi: statsby dfdx_ltfpphy=(el(e(dfdx),1,1)) dfdx_dshk99=(el(e(dfdx),1,2)) se_ltfpphy=(el(e(se_dfdx),1,1)) se_dshk99=(el(e(se_dfdx),1,2)), by(_impute_) saving(dprobit_tfpq_dshk99, replace): dprobit fdeath ltfpphy dshk99 i.year*i.prod , cluster(ppn)

**** now control for capital

xi: statsby dfdx_ltfp=(el(e(dfdx),1,1)) dfdx_lk=(el(e(dfdx),1,2)) se_ltfp=(el(e(se_dfdx),1,1)) se_lk=(el(e(se_dfdx),1,2)), by(_impute_) saving(dprobit_ltfp_k, replace): dprobit fdeath ltfp lk i.year*i.prod , cluster(ppn)

xi: statsby dfdx_ltfp2=(el(e(dfdx),1,1)) dfdx_lk=(el(e(dfdx),1,2)) se_ltfp2=(el(e(se_dfdx),1,1)) se_lk=(el(e(se_dfdx),1,2)), by(_impute_) saving(dprobit_ltfp2_k, replace): dprobit fdeath ltfp2 lk i.year*i.prod , cluster(ppn)

xi: statsby dfdx_ltfpphy=(el(e(dfdx),1,1)) dfdx_lk=(el(e(dfdx),1,2)) se_ltfpphy=(el(e(se_dfdx),1,1)) se_lk=(el(e(se_dfdx),1,2)), by(_impute_) saving(dprobit_ltfpq_k, replace): dprobit fdeath ltfpphy lk i.year*i.prod , cluster(ppn)

xi: statsby dfdx_lprice2=(el(e(dfdx),1,1)) dfdx_lk=(el(e(dfdx),1,2)) se_lprice2=(el(e(se_dfdx),1,1)) se_lk=(el(e(se_dfdx),1,2)), by(_impute_) saving(dprobit_lprice2_k, replace): dprobit fdeath lprice lk i.year*i.prod , cluster(ppn)


xi: statsby dfdx_dshk99=(el(e(dfdx),1,1)) dfdx_lk=(el(e(dfdx),1,2)) se_dshk99=(el(e(se_dfdx),1,1)) se_lk=(el(e(se_dfdx),1,2)), by(_impute_) saving(dprobit_dshk99_k, replace): dprobit fdeath dshk99 lk i.year*i.prod , cluster(ppn)

xi: statsby dfdx_ltfpphy=(el(e(dfdx),1,1)) dfdx_lprice2=(el(e(dfdx),1,2)) dfdx_lk=(el(e(dfdx),1,3)) se_ltfpphy=(el(e(se_dfdx),1,1)) se_lprice2=(el(e(se_dfdx),1,2)) se_lk=(el(e(se_dfdx),1,3)), by(_impute_) saving(dprobit_ltfpq_p_k, replace): dprobit fdeath ltfpphy lprice2 lk i.year*i.prod , cluster(ppn)


xi: statsby dfdx_ltfpphy=(el(e(dfdx),1,1)) dfdx_dshk99=(el(e(dfdx),1,2)) dfdx_lk=(el(e(dfdx),1,3)) se_ltfpphy=(el(e(se_dfdx),1,1)) se_dshk99=(el(e(se_dfdx),1,2)) se_lk=(el(e(se_dfdx),1,3)), by(_impute_) saving(dprobit_TFPQ_dshk99_K, replace): dprobit fdeath ltfpphy dshk99 lk i.year*i.prod , cluster(ppn)


clear

** "LTFP only"

use dprobit_ltfp
list dfdx_ltfp se_ltfp

** Calculate mean marginal effect, s.e., and t-stat for ltfp 

summarize dfdx_ltfp
gen Qbar = r(mean)    /* mean marginal effect estimate */
gen B = r(Var)        /* between-imputation variance of marginal effect */
gen U = se_ltfp^2  /* within-imputation variance of marginal effect */
su U
gen Ubar = r(mean)
gen T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
gen rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_ltfp = Qbar/rootT    /* t-stat for marginal effect */
su t_ltfp


clear

** "LTFP2 only"

use dprobit_ltfp2
list dfdx_ltfp2 se_ltfp2

** Calculate mean marginal effect, s.e., and t-stat for ltfp2 

summarize dfdx_ltfp2
gen Qbar = r(mean)    /* mean marginal effect estimate */
gen B = r(Var)        /* between-imputation variance of marginal effect */
gen U = se_ltfp2^2  /* within-imputation variance of marginal effect */
su U
gen Ubar = r(mean)
gen T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
gen rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_ltfp2 = Qbar/rootT    /* t-stat for marginal effect */
su t_ltfp2

clear

** "LTFPPHY only"

use dprobit_ltfpq
list dfdx_ltfpphy se_ltfpphy

** Calculate mean marginal effect, s.e., and t-stat for ltfpphy 

summarize dfdx_ltfpphy
gen Qbar = r(mean)    /* mean marginal effect estimate */
gen B = r(Var)        /* between-imputation variance of marginal effect */
gen U = se_ltfpphy^2  /* within-imputation variance of marginal effect */
su U
gen Ubar = r(mean)
gen T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
gen rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_ltfpphy = Qbar/rootT    /* t-stat for marginal effect */
su t_ltfpphy

clear

** "Lprice2 only"

use dprobit_lprice2
list dfdx_lprice2 se_lprice2

** Calculate mean marginal effect, s.e., and t-stat for lprice2 

summarize dfdx_lprice2
gen Qbar = r(mean)    /* mean marginal effect estimate */
gen B = r(Var)        /* between-imputation variance of marginal effect */
gen U = se_lprice2^2  /* within-imputation variance of marginal effect */
su U
gen Ubar = r(mean)
gen T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
gen rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_lprice2 = Qbar/rootT    /* t-stat for marginal effect */
su t_lprice2


clear

** "dshk99 only"

use dprobit_dshk99
list dfdx_dshk99 se_dshk99

** Calculate mean marginal effect, s.e., and t-stat for dshk99 (demand shock)

summarize dfdx_dshk99
gen Qbar = r(mean)    /* mean marginal effect estimate */
gen B = r(Var)        /* between-imputation variance of marginal effect */
gen U = se_dshk99^2  /* within-imputation variance of marginal effect */
su U
gen Ubar = r(mean)
gen T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
gen rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_dshk99 = Qbar/rootT    /* t-stat for marginal effect */
su t_dshk99


clear 

**"LTPPHY and LPRICE2"

use dprobit_tfpq_lprice2
list dfdx_ltfpphy se_ltfpphy dfdx_lprice2 se_lprice2

** Calculate mean marginal effect, s.e., and t-stat for dfdx_ltfpphy

summarize dfdx_ltfpphy
gen Qbar = r(mean)    /* mean marginal effect estimate */
gen B = r(Var)        /* between-imputation variance of marginal effect */
gen U = se_ltfpphy^2  /* within-imputation variance of marginal effect */
su U
gen Ubar = r(mean)
gen T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
gen rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_ltfpphy = Qbar/rootT    /* t-stat for marginal effect */
su t_ltfpphy

** Calculate mean marginal effect, s.e., and t-stat for lprice2
summarize dfdx_lprice2
replace Qbar = r(mean)    /* mean marginal effect estimate */
replace B = r(Var)        /* between-imputation variance of marginal effect */
replace U = se_lprice2^2  /* within-imputation variance of marginal effect */
su U
replace Ubar = r(mean)
replace T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
replace rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_lprice2 = Qbar/rootT    /* t-stat for marginal effect */
su t_lprice2


clear 

**"LTPPHY and dshk99"

use dprobit_tfpq_dshk99
list dfdx_ltfpphy se_ltfpphy dfdx_dshk99 se_dshk99

** Calculate mean marginal effect, s.e., and t-stat for dfdx_ltfpphy

summarize dfdx_ltfpphy
gen Qbar = r(mean)    /* mean marginal effect estimate */
gen B = r(Var)        /* between-imputation variance of marginal effect */
gen U = se_ltfpphy^2  /* within-imputation variance of marginal effect */
su U
gen Ubar = r(mean)
gen T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
gen rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_ltfpphy = Qbar/rootT    /* t-stat for marginal effect */
su t_ltfpphy

** Calculate mean marginal effect, s.e., and t-stat for demand shock
summarize dfdx_dshk99
replace Qbar = r(mean)    /* mean marginal effect estimate */
replace B = r(Var)        /* between-imputation variance of marginal effect */
replace U = se_dshk99^2  /* within-imputation variance of marginal effect */
su U
replace Ubar = r(mean)
replace T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
replace rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_dshk99 = Qbar/rootT    /* t-stat for marginal effect */
su t_dshk99

clear

* now control for capital



**"LTFP & K only"

use dprobit_ltfp_k
list dfdx_ltfp se_ltfp dfdx_lk se_lk

** Calculate mean marginal effect, s.e., and t-stat for ltfp 

summarize dfdx_ltfp
gen Qbar = r(mean)    /* mean marginal effect estimate */
gen B = r(Var)        /* between-imputation variance of marginal effect */
gen U = se_ltfp^2  /* within-imputation variance of marginal effect */
su U
gen Ubar = r(mean)
gen T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
gen rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_ltfp = Qbar/rootT    /* t-stat for marginal effect */
su t_ltfp

** Calculate mean marginal effect, s.e., and t-stat for capital
summarize dfdx_lk
replace Qbar = r(mean)    /* mean marginal effect estimate */
replace B = r(Var)        /* between-imputation variance of marginal effect */
replace U = se_lk^2  /* within-imputation variance of marginal effect */
su U
replace Ubar = r(mean)
replace T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
replace rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_lk = Qbar/rootT    /* t-stat for marginal effect */
su t_lk


clear

**"LTFP2 & K only"

use dprobit_ltfp2_k
list dfdx_ltfp2 se_ltfp2 dfdx_lk se_lk

** Calculate mean marginal effect, s.e., and t-stat for ltfp2 

summarize dfdx_ltfp2
gen Qbar = r(mean)    /* mean marginal effect estimate */
gen B = r(Var)        /* between-imputation variance of marginal effect */
gen U = se_ltfp2^2  /* within-imputation variance of marginal effect */
su U
gen Ubar = r(mean)
gen T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
gen rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_ltfp2 = Qbar/rootT    /* t-stat for marginal effect */
su t_ltfp2

** Calculate mean marginal effect, s.e., and t-stat for capital
summarize dfdx_lk
replace Qbar = r(mean)    /* mean marginal effect estimate */
replace B = r(Var)        /* between-imputation variance of marginal effect */
replace U = se_lk^2  /* within-imputation variance of marginal effect */
su U
replace Ubar = r(mean)
replace T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
replace rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_lk = Qbar/rootT    /* t-stat for marginal effect */
su t_lk

clear

**"LTFPPHY & K only"

use dprobit_ltfpq_k
list dfdx_ltfpphy se_ltfpphy dfdx_lk se_lk

** Calculate mean marginal effect, s.e., and t-stat for ltfpphy 

summarize dfdx_ltfpphy
gen Qbar = r(mean)    /* mean marginal effect estimate */
gen B = r(Var)        /* between-imputation variance of marginal effect */
gen U = se_ltfpphy^2  /* within-imputation variance of marginal effect */
su U
gen Ubar = r(mean)
gen T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
gen rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_ltfpphy = Qbar/rootT    /* t-stat for marginal effect */
su t_ltfpphy

** Calculate mean marginal effect, s.e., and t-stat for capital
summarize dfdx_lk
replace Qbar = r(mean)    /* mean marginal effect estimate */
replace B = r(Var)        /* between-imputation variance of marginal effect */
replace U = se_lk^2  /* within-imputation variance of marginal effect */
su U
replace Ubar = r(mean)
replace T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
replace rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_lk = Qbar/rootT    /* t-stat for marginal effect */
su t_lk


clear

**"Lprice2 & K only"

use dprobit_lprice2_k
list dfdx_lprice2 se_lprice2 dfdx_lk se_lk

** Calculate mean marginal effect, s.e., and t-stat for lprice2 

summarize dfdx_lprice2
gen Qbar = r(mean)    /* mean marginal effect estimate */
gen B = r(Var)        /* between-imputation variance of marginal effect */
gen U = se_lprice2^2  /* within-imputation variance of marginal effect */
su U
gen Ubar = r(mean)
gen T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
gen rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_lprice2 = Qbar/rootT    /* t-stat for marginal effect */
su t_lprice2

** Calculate mean marginal effect, s.e., and t-stat for capital
summarize dfdx_lk
replace Qbar = r(mean)    /* mean marginal effect estimate */
replace B = r(Var)        /* between-imputation variance of marginal effect */
replace U = se_lk^2  /* within-imputation variance of marginal effect */
su U
replace Ubar = r(mean)
replace T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
replace rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_lk = Qbar/rootT    /* t-stat for marginal effect */
su t_lk



clear


**"dshk99 & K only"

use dprobit_dshk99_k
list dfdx_dshk99 se_dshk99 dfdx_lk se_lk

** Calculate mean marginal effect, s.e., and t-stat for dshk99 

summarize dfdx_dshk99
gen Qbar = r(mean)    /* mean marginal effect estimate */
gen B = r(Var)        /* between-imputation variance of marginal effect */
gen U = se_dshk99^2  /* within-imputation variance of marginal effect */
su U
gen Ubar = r(mean)
gen T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
gen rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_dshk99 = Qbar/rootT    /* t-stat for marginal effect */
su t_dshk99

** Calculate mean marginal effect, s.e., and t-stat for capital
summarize dfdx_lk
replace Qbar = r(mean)    /* mean marginal effect estimate */
replace B = r(Var)        /* between-imputation variance of marginal effect */
replace U = se_lk^2  /* within-imputation variance of marginal effect */
su U
replace Ubar = r(mean)
replace T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
replace rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_lk = Qbar/rootT    /* t-stat for marginal effect */
su t_lk


clear


** "LTFPPHY and LPRICE2 &K "

use dprobit_ltfpq_p_k
list dfdx_ltfpphy se_ltfpphy dfdx_lprice2 se_lprice2 dfdx_lk se_lk

** Calculate mean marginal effect, s.e., and t-stat for ltfpphy
summarize dfdx_ltfpphy
gen Qbar = r(mean)    /* mean marginal effect estimate */
gen B = r(Var)        /* between-imputation variance of marginal effect */
gen U = se_ltfpphy^2  /* within-imputation variance of marginal effect */
su U
gen Ubar = r(mean)
gen T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
gen rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_ltfpphy = Qbar/rootT    /* t-stat for marginal effect */
su t_ltfpphy

** Calculate mean marginal effect, s.e., and t-stat for lprice2
summarize dfdx_lprice2
replace Qbar = r(mean)    /* mean marginal effect estimate */
replace B = r(Var)        /* between-imputation variance of marginal effect */
replace U = se_lprice2^2  /* within-imputation variance of marginal effect */
su U
replace Ubar = r(mean)
replace T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
replace rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_lprice2 = Qbar/rootT    /* t-stat for marginal effect */
su t_lprice2

** Calculate mean marginal effect, s.e., and t-stat for log capital
summarize dfdx_lk
replace Qbar = r(mean)    /* mean marginal effect estimate */
replace B = r(Var)        /* between-imputation variance of marginal effect */
replace U = se_lk^2  /* within-imputation variance of marginal effect */
su U
replace Ubar = r(mean)
replace T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
replace rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_lk = Qbar/rootT    /* t-stat for marginal effect */
su t_lk



clear


** "LTFPPHY and dshk99 &K "

use dprobit_TFPQ_dshk99_K
list dfdx_ltfpphy se_ltfpphy dfdx_dshk99 se_dshk99 dfdx_lk se_lk

** Calculate mean marginal effect, s.e., and t-stat for ltfpphy
summarize dfdx_ltfpphy
gen Qbar = r(mean)    /* mean marginal effect estimate */
gen B = r(Var)        /* between-imputation variance of marginal effect */
gen U = se_ltfpphy^2  /* within-imputation variance of marginal effect */
su U
gen Ubar = r(mean)
gen T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
gen rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_ltfpphy = Qbar/rootT    /* t-stat for marginal effect */
su t_ltfpphy

** Calculate mean marginal effect, s.e., and t-stat for demand shock
summarize dfdx_dshk99
replace Qbar = r(mean)    /* mean marginal effect estimate */
replace B = r(Var)        /* between-imputation variance of marginal effect */
replace U = se_dshk99^2  /* within-imputation variance of marginal effect */
su U
replace Ubar = r(mean)
replace T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
replace rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_dshk99 = Qbar/rootT    /* t-stat for marginal effect */
su t_dshk99

** Calculate mean marginal effect, s.e., and t-stat for log capital
summarize dfdx_lk
replace Qbar = r(mean)    /* mean marginal effect estimate */
replace B = r(Var)        /* between-imputation variance of marginal effect */
replace U = se_lk^2  /* within-imputation variance of marginal effect */
su U
replace Ubar = r(mean)
replace T = Ubar + (1 + 1/500)*B  /* Total variance of marginal effect */
replace rootT = sqrt(T)           /* Total standard error */
su T
su rootT
gen t_lk = Qbar/rootT    /* t-stat for marginal effect */
su t_lk

